home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 001a / mskrmsrc.zip / MSNUT1.ASM < prev    next >
Assembly Source File  |  1991-10-24  |  12KB  |  590 lines

  1.        NAME    MSNUT1
  2. ; File MSNUT1.ASM
  3. ; Provides various Intel 8088 level operations, including
  4. ; display facilities (via msg buffer or DOS) for char, strings, etc.
  5. ;
  6. ; Copyright 1991, University of Waterloo.
  7. ; Copyright (C) 1991, Trustees of Columbia University in the
  8. ;  City of New York.  Permission is granted to any individual or
  9. ;  institution to use, copy, or redistribute this software as long as
  10. ;  it is not sold for profit and this copyright notice is retained.
  11. ; Written by Erick Engelke of the University of Waterloo, Waterloo, 
  12. ;  Ontario, Canada,
  13. ;  and by Joe R. Doupnik, Utah State University, 
  14. ;  jrd@cc.usu.edu, jrd@usu.Bitnet.
  15. ;
  16. ; Edit history
  17. ; 6 Sept 1991 v3.11
  18. ; Last Edit
  19. ; 6 Sept 1991
  20.  
  21. cr    equ    0dh
  22. lf    equ    0ah
  23. conout    equ    2
  24. dos    equ    21h
  25.  
  26. BIOSCLK    equ    046ch
  27. HI_MAX    equ    018h
  28. LO_MAX    equ     0b0h
  29.  
  30. _TEXT    SEGMENT  WORD PUBLIC 'CODE'
  31. _TEXT    ENDS
  32. _DATA    SEGMENT  WORD PUBLIC 'DATA'
  33. _DATA    ENDS
  34. CONST    SEGMENT  WORD PUBLIC 'CONST'
  35. CONST    ENDS
  36. _BSS    SEGMENT  WORD PUBLIC 'BSS'
  37. _BSS    ENDS
  38. DGROUP    GROUP    CONST, _BSS, _DATA
  39.     ASSUME  CS: _TEXT, DS: DGROUP, SS: DGROUP
  40.  
  41. _DATA    SEGMENT
  42.     extrn    _doslevel:word, _msgcnt:word, _msgbuf:byte,_display_mode:byte
  43. _DATA    ENDS
  44.  
  45. _TEXT    segment
  46.  
  47.         ; Compute 1's-complement sum of data buffer
  48.         ; unsigned checksum( unsigned word *buf, unsigned cnt)
  49.  
  50.     public    _checksum
  51. _checksum proc    near
  52.     push    bp
  53.     mov    bp,sp
  54.     push    si
  55.     push    bx
  56.     push    cx
  57.     push    dx
  58.         mov     si,[bp+4+0]        ; offset of data to be checked
  59.     mov     cx,[bp+4+2]        ; cx = cnt
  60.     mov    bl,cl
  61.     shr     cx,1            ; group into words
  62.     xor    dx,dx            ; set checksum to 0
  63.         cld
  64.     jcxz    chk3
  65.         clc
  66. chk1:    lodsw
  67.     adc    dx,ax
  68.     loop    chk1
  69.                     ; resolve remainder
  70. chk2:    adc     dx,0
  71.         jc    chk2
  72. chk3:    and    bl,1
  73.     jz    chk5
  74.         xor     ah,ah
  75.     lodsb
  76.         clc
  77.         add     dx,ax
  78. chk4:    adc     dx,0
  79.     jc    chk4
  80. chk5:    mov    ax,dx            ; result into ax
  81.         or      ax,ax
  82.     jnz    chk6
  83.         mov     ax,0ffffh
  84. chk6:    pop    dx
  85.     pop    cx
  86.     pop    bx
  87.     pop    si
  88.     pop    bp
  89.     ret
  90. _checksum endp
  91.  
  92. ;  intel - convert intel <--> bigendian
  93. ;
  94. ;*************************************************************************
  95. ;  USAGE:  ULONG intel( ULONG val )
  96. ;          - convert to intel format
  97. ;*************************************************************************
  98.     public    _intel
  99. _intel    proc    near
  100.     push    bp
  101.     mov    bp,sp
  102.     mov    AX,[BP+4+2]
  103.     mov    DX,[BP+4+0]
  104.     xchg    AL, AH
  105.     xchg    DL, DH
  106.     pop    bp
  107.     ret
  108. _intel    endp
  109. ;*************************************************************************
  110. ;  USAGE:  UINT intel16( UINT val )
  111. ;          - convert to intel format
  112. ;*************************************************************************
  113.     public    _intel16
  114. _intel16 proc    near
  115.     push    bp
  116.     mov    bp,sp
  117.     mov    AX,[BP+4+0]
  118.     xchg    AL,AH
  119.     pop    bp
  120.     ret
  121. _intel16 endp
  122.  
  123. ; int ourmod(int top, int bottom)
  124.     public    _ourmod
  125. _ourmod    proc    near
  126.     push    bp
  127.     mov    bp,sp
  128.     push    bx
  129.     mov    ax,[bp+4+0]        ; top number
  130.     mov    bx,[bp+4+2]        ; bottom number (radix)
  131.     xor    dx,dx
  132.     or    bx,bx            ; bottom is zero?
  133.     jz    ourmod1            ; yes, return zero
  134.     div    bx
  135. ourmod1:mov    ax,dx            ; return remainder
  136.     pop    bx
  137.     pop    bp
  138.     ret
  139. _ourmod    endp
  140.  
  141. ; int ourdiv(int top, int bottom)
  142.     public    _ourdiv
  143. _ourdiv    proc    near
  144.     push    bp
  145.     mov    bp,sp
  146.     push    bx
  147.     mov    ax,[bp+4+0]        ; top
  148.     mov    bx,[bp+4+2]        ; bottom
  149.     xor    dx,dx
  150.     or    bx,bx            ; divide by zero?
  151.     jz    outdiv1            ; z = yes, divide by one
  152.     div    bx
  153. outdiv1:pop    bx
  154.     pop    bp            ; quotient is returned in ax
  155.     ret
  156. _ourdiv    endp
  157.  
  158. ; int ourlmod(long top, int bottom)
  159.     public    _ourlmod
  160. _ourlmod proc    near
  161.     push    bp
  162.     mov    bp,sp
  163.     push    bx
  164.     mov    ax,[bp+4+0]        ; top lower 16 bits
  165.     mov    dx,[bp+4+2]        ; top upper 16 bits
  166.     mov    bx,[bp+4+4]        ; bottom
  167.     or    bx,bx            ; zero?
  168.     jz    outlmo1            ; z = yes divide by 2^16 and quit
  169.     div    bx
  170. outlmo1:mov    ax,dx            ; return remainder in ax
  171.     pop    bx
  172.     pop    bp
  173.     ret
  174. _ourlmod endp
  175.  
  176. ; int ourldiv(long top, int bottom)
  177.     public    _ourldiv
  178. _ourldiv proc    near
  179.     push    bp
  180.     mov    bp,sp
  181.     push    bx
  182.     mov    ax,[bp+4+0]        ; top lower 16 bits
  183.     mov    dx,[bp+4+2]        ; top upper 16 bits
  184.     mov    bx,[bp+4+4]        ; bottom
  185.     cmp    dx,bx            ; about to overflow?
  186.     jae    ourldiv1        ; ae = yes, return 0xffffh
  187.     div    bx
  188.     xor    dx,dx            ; clear remainder (high order ret)
  189.     pop    bx
  190.     pop    bp            ; quotient is returned in dx:ax
  191.     ret
  192. ourldiv1:mov    ax,0ffffh        ; overflow indication
  193.     xor    dx,dx            ; clear high order
  194.     pop    bx
  195.     pop    bp
  196.     ret
  197. _ourldiv endp
  198.  
  199. ; long ourlmul(long top, int bottom)
  200.     public    _ourlmul
  201. _ourlmul proc    near
  202.     push    bp
  203.     mov    bp,sp
  204.     push    bx
  205.     push    cx
  206.     mov    ax,[bp+4+2]        ; top upper 16 bits
  207.     mov    bx,[bp+4+4]        ; bottom
  208.     mul    bx
  209.     mov    cx,ax            ; save product (no overflow noted)
  210.     mov    ax,[bp+4+0]        ; top lower 16 bits
  211.     mul    bx
  212.     adc    dx,cx            ; new upper 16 bits
  213.     pop    cx
  214.     pop    bx
  215.     mov    sp,bp
  216.     pop    bp            ; long product is returned in dx:ax
  217.     ret
  218. _ourlmul endp
  219.  
  220. ; void * bcopy(src, dest, count)
  221. ; void *dest, *src;
  222. ; size_t count;
  223. ; copy count bytes from src to dest
  224.     public    _bcopy
  225. _bcopy proc    near
  226.     push    bp
  227.     mov    bp,sp
  228.     push    es
  229.     push    si
  230.     push    di
  231.     push    cx
  232.     mov    ax,ds            ; set to same data segment
  233.     mov    es,ax
  234.     mov    si,[bp+4+0]        ; offset of source
  235.     mov    di,[bp+4+2]        ; offset of destination
  236.     mov    cx,[bp+4+4]        ; count
  237.     cld
  238.     push    di            ; push dest address for return
  239.     jcxz    bcopy2            ; z = nothing to copy
  240.     or    di,di            ; is destination NULL?
  241.     jz    bcopy2            ; z = yes, don't do a thing
  242.     cmp    si,di            ; is source after destination?
  243.     ja    bcopy1            ; a = yes, no overlap problem
  244.     je    bcopy2            ; e = same place, do nothing
  245.     add    di,cx            ; start at the ends
  246.     dec    di
  247.     add    si,cx
  248.     dec    si
  249.     std                ; work backward
  250. bcopy1:    rep    movsb
  251. bcopy2:    pop    ax            ; recover return destination
  252.     cld
  253.     pop    cx
  254.     pop    di
  255.     pop    si
  256.     pop    es
  257.     mov    sp,bp
  258.     pop    bp
  259.     ret
  260. _bcopy endp
  261.  
  262. ; void * memset(dest, c, count)
  263. ; void *dest;
  264. ; char c;
  265. ; size_t count;
  266.     public    _memset
  267. _memset    proc    near
  268.     push    bp
  269.     mov    bp,sp
  270.     push    es
  271.     push    di
  272.     push    cx
  273.     mov    ax,ds            ; setup data segment
  274.     mov    es,ax
  275.     mov    di,[bp+4+0]        ; offset of destination
  276.     or    di,di            ; is it NULL?
  277.     jz    memset1            ; z = yes, don't do a thing
  278.     push    di            ; save dest for return
  279.     mov    al,[bp+4+2]        ; byte of character c
  280.     mov    ah,al
  281.     mov    cx,[bp+4+4]        ; count
  282.     jcxz    memset1            ; z = do nothing
  283.     cld
  284.     shr    cx,1
  285.     jnc    memset2
  286.     stosb
  287. memset2:rep    stosw
  288.     pop    ax            ; return pointer to destination
  289. memset1:pop    cx
  290.     pop    di
  291.     pop    es
  292.     mov    sp,bp
  293.     pop    bp
  294.     ret
  295. _memset    endp
  296.  
  297. ;
  298. ;  Timer routines - use set_*timeout to receive a clock value which
  299. ;                   can later be tested by calling chk_timeout with
  300. ;                   that clock value to determine if a timeout has
  301. ;                   occurred.  chk_timeout returns 0 if NOT timed out.
  302. ;
  303. ;
  304. ;  Usage :
  305. ;           long set_timeout( int seconds );
  306. ;           long set_ttimeout( int pc_ticks );
  307. ;           int chk_timeout( long value );
  308. ;
  309. ;  (c) 1990 University of Waterloo,
  310. ;           Faculty of Engineering,
  311. ;           Engineering Microcomputer Network Development Office
  312. ;
  313. ;  version
  314. ;
  315. ;    0.1    7-Nov -1990   E. P. Engelke
  316. ;
  317. ;
  318.  
  319.     public    _set_ttimeout
  320. _set_ttimeout proc    near
  321.     push    bp
  322.     mov    bp,sp
  323.     push    es
  324.         xor     AX, AX
  325.         cwd
  326.         mov     ES, AX
  327.         mov     AX, [BP+4+0]
  328.     pushf
  329.     cli
  330.         add     AX, ES:[BIOSCLK+0];
  331.         adc     DX, ES:[BIOSCLK+2];
  332.     popf
  333.     pop    es
  334.     pop    bp
  335.     ret
  336. _set_ttimeout endp
  337.  
  338.     public    _set_timeout
  339. _set_timeout proc near
  340.     push    bp
  341.     mov    bp,sp
  342.     push    es
  343.     push    cx
  344.     xor    AX, AX            ; reference low memory
  345.     mov    ES, AX
  346.  
  347.     mov    AX,[BP+4+0]        ; seconds
  348.     xor    dx,dx
  349.     mov    cx,1165
  350.     mul    cx            ; 1165/64 = 18.203...
  351.     mov    CX,6
  352. tmp:    shr    DX,1
  353.     rcr    AX,1
  354.     loop    tmp
  355.     pushf
  356.     cli
  357.     add    AX,ES:[BIOSCLK+0]
  358.     adc    DX,ES:[BIOSCLK+2]
  359.     popf
  360.     pop    cx
  361.     pop    es
  362.     pop    bp
  363.     ret
  364. _set_timeout    endp
  365.  
  366.     public    _chk_timeout
  367. _chk_timeout    proc near
  368.     push    bp
  369.     mov    bp,sp
  370.     push    cx
  371.     push    es
  372.     xor    AX, AX
  373.     mov    ES, AX
  374.     pushf
  375.     cli
  376.     mov    CX,ES:[BIOSCLK+0]
  377.     mov    BX,ES:[BIOSCLK+2]
  378.     popf
  379.     pop    es
  380.     mov    AX,[BP+4+0]        ; timeout value
  381.     mov    DX,[BP+4+2]
  382.     cmp    DX,BX            ; if timeout < clock, has expired
  383.         jb      ret_tru
  384.     cmp    AX, CX
  385.         jbe      ret_tru
  386.                     ; may have gone over by one day
  387.     sub    AX, LO_MAX
  388.     sbb    DX, HI_MAX
  389.     jc    ret_fal            ; c = nope, timeout is today
  390.                     ; test timeout new values
  391.     cmp    DX, BX
  392.     jb    ret_tru
  393.     cmp    AX, CX
  394.     ja    ret_fal
  395. ret_tru:mov    AX, 1            ; say have timed out
  396.     pop    cx
  397.     pop    bp
  398.     ret
  399.  
  400. ret_fal:xor    AX, AX            ; say have not timed out
  401.     pop    cx
  402.     pop    bp
  403.     ret
  404. _chk_timeout    endp
  405.  
  406. ; unsigned long realclock(void)
  407.     public    _realclock
  408. _realclock    proc near        ; get Bios time of day dword
  409.     push    es
  410.     xor    ax,ax            ; reference low memory
  411.     mov    es,ax
  412.     pushf
  413.     cli
  414.     mov    ax,es:[BIOSCLK+0]    ; return Bios time of day tick count
  415.     mov    dx,es:[BIOSCLK+2]
  416.     popf
  417.     pop    es
  418.     ret
  419. _realclock endp
  420.  
  421. ; void _chkstk()
  422. ; {
  423. ;  ;
  424. ; }
  425.     public    __chkstk, __aNchkstk
  426. __chkstk proc    near            ; MSC v5.1
  427. __aNchkstk equ    this byte        ; MSC v6.00
  428.     pop    bx            ; pop return address
  429.     sub    sp,ax            ; down adjust stack pointer
  430.     jmp    bx            ; return the no-stack way
  431. __chkstk endp
  432.  
  433.  
  434. ; void 
  435. ; outch(char ch)
  436. ; Sends character to the screen via the msgbuf buffer if operating at
  437. ; interrupt level, or via DOS if operating at task level.
  438.     public    _outch
  439. _outch    proc    near
  440.     push    bp
  441.     mov    bp,sp
  442.     push    ax
  443.     push    bx
  444.     cmp    _display_mode,0        ; quiet screen?
  445.     je    outch3            ; e = yes
  446.     mov    al,[bp+4]        ; get the character
  447.     cmp    _doslevel,0        ; at DOS task level?
  448.     je    outch1            ; e = no
  449.     mov    ah,conout        ; use DOS
  450.     push    dx
  451.     mov    dl,al
  452.     int    dos
  453.     pop    dx
  454.     jmp    short outch3
  455. outch1:    cmp    _msgcnt,256        ; is buffer filled?
  456.     ja    outch3            ; a = yes, discard this byte
  457.     mov    bx,_msgcnt
  458.     mov    _msgbuf[bx],al
  459.     inc    _msgcnt
  460. outch3:    pop    bx
  461.     pop    ax
  462.     mov    sp,bp
  463.     pop    bp
  464.     ret
  465. _outch    endp
  466.  
  467.  
  468. ; void 
  469. ; outsn(char * string, int count)
  470. ; display counted string
  471.     public    _outsn
  472. _outsn    proc    near
  473.     push    bp
  474.     mov    bp,sp
  475.     push    ax
  476.     push    si
  477.     mov    si,[bp+4]        ; string address
  478.     mov    cx,[bp+4+2]        ; string length
  479.     cld
  480. outsn1:    lodsb
  481.     or    al,al
  482.         jz      outsn2
  483.         cmp     al,cr            ; convert cr to cr lf
  484.         jnz     outsn3
  485.         push    ax
  486.         call    _outch
  487.         pop     ax
  488. outsn3: push    ax
  489.         call    _outch
  490.         pop     ax
  491.     loop    outsn1
  492. outsn2:    pop    si
  493.     pop    ax
  494.     mov    sp,bp
  495.     pop    bp
  496.     ret
  497. _outsn    endp
  498.  
  499. ; void 
  500. ; outs(char * string)
  501. ; display asciiz string
  502.     public    _outs
  503. _outs    proc    near
  504.     push    bp
  505.     mov    bp,sp
  506.     push    ax
  507.     push    si
  508.     mov    si,[bp+4]        ; asciiz string address
  509.     cld
  510. outs1:    lodsb
  511.         or      al,al
  512.     jz     outs2
  513.         cmp     al,cr            ; convert cr to cr lf
  514.         jnz     outs3
  515.         push    ax
  516.         call    _outch
  517.         pop     ax
  518. outs3:  push    ax
  519.         call    _outch
  520.         pop     ax
  521.     jmp    outs1
  522. outs2:    pop    si
  523.     pop    ax
  524.     mov    sp,bp
  525.     pop    bp
  526.     ret
  527. _outs    endp
  528.  
  529. ; void
  530. ; outhex(char c)
  531. ; display char in hex
  532.     public _outhex
  533. _outhex    proc    near
  534.     push    bp
  535.     mov    bp,sp
  536.     push    ax
  537.         mov     ax,[bp+4]        ; incoming character
  538.         mov     cl, 4
  539.         shr     al, cl
  540.         call    outh
  541.         mov     ax,[bp+4]
  542.         call    outh
  543.     pop    ax
  544.     mov    sp,bp
  545.     pop    bp
  546.     ret
  547.  
  548. ; worker for outhex
  549. outh    proc    near
  550.     and     al,0fh
  551.         cmp     al,9
  552.         jbe     outh1
  553.         add     al,'A' - '9' - 1
  554. outh1:    add     al,'0'
  555.         push    ax
  556.         call    _outch
  557.         pop     ax
  558.         ret
  559. outh    endp
  560. _outhex    endp
  561.  
  562. ; output a string of hex chars
  563. ; void
  564. ; outhexes(char * string, int count )
  565.     public    _outhexes
  566. _outhexes proc    near
  567.     push    bp
  568.     mov    bp,sp
  569.     push    ax
  570.     push    si            ; preserve such things
  571.     mov    si,[bp+4]        ; get string pointer
  572.     mov    cx,[bp+4+2]        ; get char count
  573.     cld
  574. outhexs1:lodsb                ; read a byte
  575.     push    cx            ; save loop counter
  576.     push    ax
  577.     call    _outhex            ; display as hex pair
  578.     add    sp,2            ; clean stack
  579.     pop    cx
  580.     loop    outhexs1        ; do count's worth
  581.     pop    si
  582.     pop    ax
  583.     mov    sp,bp
  584.     pop    bp
  585.     ret
  586. _outhexes endp
  587. _TEXT    ends
  588.         end
  589.